package com.mars.admin.modules.post.service.impl;

import com.mars.admin.modules.base.service.impl.BaseServiceImpl;
import com.mars.admin.modules.post.entity.PostCommentLike;
import com.mars.admin.modules.post.mapper.PostCommentLikeMapper;
import com.mars.admin.modules.post.service.IPostCommentLikeService;
import com.mars.admin.modules.post.service.IPostCommentService;
import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 帖子评论点赞服务实现类
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@Service
public class PostCommentLikeServiceImpl extends BaseServiceImpl<PostCommentLike> implements IPostCommentLikeService {

    @Autowired
    private PostCommentLikeMapper postCommentLikeMapper;
    
    @Autowired
    private IPostCommentService postCommentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean likeComment(Long commentId, Long userId) {
        try {
            // 检查是否已经点赞
            PostCommentLike existingLike = postCommentLikeMapper.selectByCommentIdAndUserId(commentId, userId);
            if (existingLike != null) {
                    return false;
            }
            
            // 创建新的点赞记录
            PostCommentLike commentLike = new PostCommentLike();
            commentLike.setCommentId(commentId);
            commentLike.setUserId(userId);
            commentLike.setCreateTime(LocalDateTime.now());
            
            boolean saved = save(commentLike);
            if (saved) {
                // 更新评论点赞数
                postCommentService.updateCommentLikeCount(commentId, 1);
            }
            
            return saved;
        } catch (Exception e) {
            log.error("点赞评论失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean unlikeComment(Long commentId, Long userId) {
        try {
            boolean deleted = postCommentLikeMapper.deleteByCommentIdAndUserId(commentId, userId) > 0;
            if (deleted) {
                // 更新评论点赞数
                postCommentService.updateCommentLikeCount(commentId, -1);
            }
            return deleted;
        } catch (Exception e) {
            log.error("取消点赞评论失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            throw e;
        }
    }

    @Override
    public boolean isLiked(Long commentId, Long userId) {
        try {
            return postCommentLikeMapper.isLiked(commentId, userId);
        } catch (Exception e) {
            log.error("检查用户是否对评论点赞失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            return false;
        }
    }

    @Override
    public PostCommentLike getUserCommentLike(Long commentId, Long userId) {
        try {
            return postCommentLikeMapper.selectByCommentIdAndUserId(commentId, userId);
        } catch (Exception e) {
            log.error("获取用户对评论的点赞记录失败，评论ID：{}，用户ID：{}", commentId, userId, e);
            throw e;
        }
    }

    @Override
    public Page<PostCommentLike> getUserLikes(Long userId, Page<PostCommentLike> page) {
        try {
            return postCommentLikeMapper.selectByUserId(userId, page);
        } catch (Exception e) {
            log.error("查询用户的点赞记录失败，用户ID：{}", userId, e);
            throw e;
        }
    }

    @Override
    public Page<PostCommentLike> getCommentLikes(Long commentId, Page<PostCommentLike> page) {
        try {
            return postCommentLikeMapper.selectByCommentId(commentId, page);
        } catch (Exception e) {
            log.error("查询评论的点赞记录失败，评论ID：{}", commentId, e);
            throw e;
        }
    }

    @Override
    public int countCommentLikes(Long commentId) {
        try {
            return postCommentLikeMapper.countByCommentId(commentId);
        } catch (Exception e) {
            log.error("统计评论的点赞数量失败，评论ID：{}", commentId, e);
            throw e;
        }
    }

    @Override
    public int countUserLikes(Long userId) {
        try {
            return postCommentLikeMapper.countByUserId(userId);
        } catch (Exception e) {
            log.error("统计用户的点赞数量失败，用户ID：{}", userId, e);
            throw e;
        }
    }


    @Override
    public List<Long> getUserLikedCommentIds(Long userId) {
        try {
            return postCommentLikeMapper.selectCommentIdsByUserId(userId);
        } catch (Exception e) {
            log.error("查询用户点赞的评论ID列表失败，用户ID：{}", userId, e);
            throw e;
        }
    }

    @Override
    public List<Long> getCommentLikeUserIds(Long commentId) {
        try {
            return postCommentLikeMapper.selectUserIdsByCommentId(commentId);
        } catch (Exception e) {
            log.error("查询评论的点赞用户ID列表失败，评论ID：{}", commentId, e);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCommentLikes(Long commentId) {
        try {
            return postCommentLikeMapper.deleteByCommentId(commentId) > 0;
        } catch (Exception e) {
            log.error("根据评论ID删除所有点赞记录失败，评论ID：{}", commentId, e);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserLikes(Long userId) {
        try {
            return postCommentLikeMapper.deleteByUserId(userId) > 0;
        } catch (Exception e) {
            log.error("根据用户ID删除所有点赞记录失败，用户ID：{}", userId, e);
            throw e;
        }
    }
} 